import React, { useState, useEffect } from 'react'
import { Col, Form, Input, Button, Table, Select, Row } from 'antd'
import { PageHeaderWrapper } from '@ant-design/pro-layout'
import RenderButtom from '@/components/RenderButton'
import SearchBox from '@/components/SearchBox'
import { getStaffList, getSupplierList, getStoreList } from '@/services/supplier/manageStaff/manageStaff'

const { Option } = Select

// 表头
const columns = [
  {
    title: '编号',
    dataIndex: 'id',
    key: 'id',
    align: 'center'
  },
  {
    title: '姓名',
    dataIndex: 'name',
    key: 'name',
    align: 'center'
  },
  {
    title: '手机号',
    dataIndex: 'phone',
    key: 'phone',
    align: 'center'
  },
  {
    title: '权限',
    dataIndex: 'roleId',
    key: 'roleId',
    align: 'center'
  },
  {
    title: '部门',
    dataIndex: 'departmentId',
    key: 'departmentId',
    align: 'center'
  },
  {
    title: '职位',
    dataIndex: 'commissionVal',
    key: 'commissionVal',
    align: 'center'
  },
  {
    title: '分组',
    dataIndex: 'groupName',
    key: 'groupName',
    align: 'center'
  },
  {
    title: '分销佣金',
    dataIndex: 'distributionCommission',
    key: 'distributionCommission',
    align: 'center'
  },
  {
    title: '转发',
    dataIndex: 'relay',
    key: 'relay',
    align: 'center'
  },
  {
    title: '曝光',
    dataIndex: 'exposure',
    key: 'exposure',
    align: 'center'
  },
  {
    title: '状态',
    dataIndex: 'status',
    key: 'status',
    align: 'center'
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    key: 'createTime',
    align: 'center'
  },
  {
    title: '更新时间',
    dataIndex: 'updateTime',
    key: 'updateTime',
    align: 'center'
  }
]

const rowSelection = {
  type: 'radio',
  onChange: (selectedRowKeys, selectedRows) => {
    setRecord(selectedRows)
    // console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
    console.log(record)
  },
}

const ManageStaff = props => {
  const { form } = props
  const { getFieldDecorator } = form
  const [staffList, setStaffList] = useState([])
  const [total, setTotal] = useState(1)
  const [pageNo, setPageNo] = useState(1)
  const [pageSize, setPageSize] = useState(10)
  const [record, setRecord] = useState({})
  const [supplierList, setSupplierList] = useState([])
  const [storeList, setStoreList] = useState([])

  useEffect(() => {
    init()
  }, [])
  const init = async () => {
    let params = {
      pageNo,
      pageSize
    }
    await getStaffLists(params)
    await getSupplierLists()
  }
  // 表单查询
  const handleSearch = e => {
    e.preventDefault()
    form.validateFields(async (err, values) => {
      console.log('Received values of form: ', values)
      let params = {
        pageNo,
        pageSize,
        staffManage: {
          name: values.name,
          phone: values.phone
        },
        supplierId: values.supplierId,
        storeId: values.storeId
      }
      await getStaffLists(params)
    })
  }
  // 表单清空
  const handleReset = e => {
    form.resetFields();
  }
  // 查询条件
  const getFields = () => {
    return (
      <Row gutter={24}>
        <Col span={12}>
          <Form.Item label="姓名" labelCol={{ span: 4 }} wrapperCol={{ span: 20 }} >
            {getFieldDecorator('name', {})
              (<Input placeholder="请输入姓名" />)}
          </Form.Item>
        </Col>
        <Col span={12}>
          <Form.Item label="手机号" labelCol={{ span: 4 }} wrapperCol={{ span: 20 }} >
            {getFieldDecorator('phone', {})
              (<Input placeholder="请输入手机号" />)}
          </Form.Item>
        </Col>
        <Col span={12}>
          <Form.Item label="所属商户" labelCol={{ span: 4 }} wrapperCol={{ span: 20 }} >
            {getFieldDecorator('supplierId', {})
              (<Select
                style={{ width: '100%' }}
                showSearch
                filterOption={(input, option) => option.props.children.indexOf(input) >= 0}
                placeholder="请选择"
                onChange={selectSupplier}
              >
                { supplierList && supplierList.map(r => <Option key={r.id} value={r.id}>{r.merchantName}</Option>)}
              </Select>)}
          </Form.Item>
        </Col>
        <Col span={12}>
          <Form.Item label="门店" labelCol={{ span: 4 }} wrapperCol={{ span: 20 }} >
            {getFieldDecorator('storeId', {})
              (<Select
                style={{ width: '100%' }}
                showSearch
                filterOption={(input, option) => option.props.children.indexOf(input) >= 0}
                placeholder="请选择"
              >
                { storeList && storeList.map(r => <Option key={r.id} value={r.id}>{r.name}</Option>)}
              </Select>)}
          </Form.Item>
        </Col>
      </Row>
    )
  }
  // 按钮处理
  const handleBtn = type => {

  }
  // 获取员工列表
  const getStaffLists = async (params) => {
    let res = await getStaffList(params)
    if (res.code === '0') {
      setStaffList(res.data)
      setTotal(res.iTotalDisplayRecords)
    } else {
      alert(res.msg)
    }
  }

  const pagination = {
    total: parseInt(total),
    onChange(page) {
      let params = {
        pageNo: page,
        pageSize
      }
      getStaffLists(params)
    }
  }
  // 选择商户
  const selectSupplier = async (id) => {
    await getStoreLists(id)
  }
  // 获取商户列表
  const getSupplierLists = async () => {
    let res = await getSupplierList()
    if (res.code === '0') {
      setSupplierList(res.data)
    } else {
      alert(res.msg)
    }
  }
  // 获取店铺列表
  const getStoreLists = async (supplierId) => {
    let params = {
      pageSize: 999,
      supplierId
    }
    let res = await getStoreList(params)
    if (res.code === '0') {
      setStoreList(res.data)
    } else {
      alert(res.msg)
    }
  }
  return (
    <PageHeaderWrapper>
      <SearchBox handleReset={handleReset} handleSearch={handleSearch} getFields={getFields} />
      <RenderButtom click={handleBtn} />
      <Table
        columns={columns}
        dataSource={staffList}
        rowKey={record => record.id}
        pagination={pagination}
        rowSelection={rowSelection}
      />
   </PageHeaderWrapper>
  )
}

export default Form.create()(ManageStaff)
